package ${package}.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils extends org.apache.commons.lang3.time.DateUtils {

    /**
     * ^yyyy-MM-dd HH:mm:ss$
     */
    public static final String REGEX_LONG_DATE_FORMAT  = "^\\d{4}-\\d{1,2}-\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}$";
    /**
     * ^yyyy-MM-dd$
     */
    public static final String REGEX_SHORT_DATE_FORMAT = "^\\d{4}-\\d{1,2}-\\d{1,2}$";
    /**
     * ^HH:mm:ss$
     */
    public static final String REGEX_SHORT_TIME_FORMAT = "^\\d{1,2}:\\d{1,2}:\\d{1,2}$";
    /**
     * ^\\d+$
     */
    public static final String REGEX_TIMESTAMP_FORMAT  = "^\\d+$";

    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public static final String LONG_DATE_FORMAT        = "yyyy-MM-dd HH:mm:ss";
    /**
     * yyyy-MM-dd
     */
    public static final String SHORT_DATE_FORMAT       = "yyyy-MM-dd";
    /**
     * HH:mm:ss
     */
    public static final String SHORT_TIME_FORMAT       = "HH:mm:ss";
    /**
     * 最小数据库有效期
     */
    public static final Date   MIN_DB_DATE;
    /**
     * 2015-01-01
     */
    public static final Date   DATE_20150101;
    static {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(1900, 1, 1);
        MIN_DB_DATE = calendar.getTime();

        SimpleDateFormat dateFormat = new SimpleDateFormat(SHORT_DATE_FORMAT);
        try {
            DATE_20150101 = dateFormat.parse("2015-01-01");
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取日期的日期部份
     * 
     * @param dt
     * @return
     */
    public static Date getDatePart(Date dt) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dt);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 格式化指定的日期
     * 
     * @param dt
     * @param pattern
     * @return
     */
    public static String formatDate(Date dt, String pattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        String str = dateFormat.format(dt);
        return str;
    }

    /**
     * 返回指定格式化的日期
     * 
     * @param dt
     * @param pattern
     * @return
     * @throws ParseException 
     */
    public static Date parseDate(String str, String pattern) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        Date date = dateFormat.parse(str);
        return date;
    }

    /**
     * 获取月份起始日期     
     * 日期部份，不包括时间
     * @param date
     * @return
     * @throws ParseException
     */
    public static Date getDayStartOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取月份结束日期
     * 日期部份，不包括时间
     * @param date
     * @return
     * @throws ParseException
     */
    public static Date getDayEndOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取指定年月日的日期
     * 
     * @param year 年
     * @param month 月
     * @param day 日
     * @return
     */
    public static Date getDate(int year, int month, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DATE, day);
        return calendar.getTime();
    }

    /**
     * 获取今天日期部分
     * 
     * @return
     */
    public static Date getToday() {
        Date dtToday = getDatePart(new Date());
        return dtToday;
    }

    /**
     * 获取指定日期的指定字段值
     * 
     * @param dt
     * @param field
     * @return
     */
    public static int getField(Date dt, int field) {
        if (null == dt) {
            throw new RuntimeException("dt不允许为空");
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dt);
        return calendar.get(field);
    }

    /**
     * 计算两个日期间的月差
     * 
     * @param dt1 小的日期
     * @param dt2 大的日期
     * @return dt2>dt1 返回正数， 否则返回负数
     */
    public static int monthOffset(Date dt1, Date dt2) {
        if (null == dt1 || null == dt2) {
            throw new RuntimeException("dt1或dt2为空");
        }
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(dt1);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(dt2);
        return (calendar2.get(Calendar.YEAR) - calendar1.get(Calendar.YEAR)) * 12
               + (calendar2.get(Calendar.MONTH) - calendar1.get(Calendar.MONTH));
    }
}
